package com.surfilter.modules.user.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.surfilter.modules.role.model.PermissionModel;
import com.surfilter.modules.user.model.ActionModel;
import com.surfilter.modules.user.model.ModuleModel;
import com.surfilter.platform.base.dao.DaoBase;

@Repository
public class PermissionDao extends DaoBase<PermissionModel> {
	@Resource(name = "surfilterSqlMapClient")
	private SqlMapClient sqlMapClient;

	@Override
	public void initSqlMapClient() {
		this.setSqlMapClient(sqlMapClient);
	}

	public void addBatchPermission(List<PermissionModel> list) {
		this.addBatch("addPermission", list);
	}

	public void delPermissionByRoleId(int roleId) {
		this.deleteObject("delPermissionByRoleId", roleId);
	}

	public void delPermissionByRoleIds(List<Integer> roleIds) {
		this.deleteBatch("delPermissionByRoleId", roleIds);
	}

	public List<ModuleModel> getAllValidModules() {
		List<ModuleModel> modules = this.getListByObject("getAllValidModulesForUserModule",
				null);
		return modules;
	}

	public List<ActionModel> getActionsByModuleId(Integer moduleId) {
		List<ActionModel> actions = this.getListByObject("getActionsByModuleIdForUserModule",
				moduleId);
		return actions;
	}

	public List<Integer> getModuleIdByUserId(Integer userId) {
		List<Integer> modules = this.getListByObject("getModuleIdByUserId",
				userId);
		return modules;
	}

	public List<Integer> getActionIdByUserId(Integer userId) {
		List<Integer> actions = this.getListByObject("getActionIdByUserId",
				userId);
		return actions;
	}

	public void delAllUserModules(int userId) {
		this.deleteObject("delAllUserModules", userId);
	}

	public void addAllUserModules(int userId, List<Integer> moduleIds,
			int updateUser, Date updateTime) {
		
		List<Map<String,Object>> params = new ArrayList<Map<String,Object>>();
		for(Integer moduleId : moduleIds){
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("createUser", updateUser);
			param.put("createTime", updateTime);
			param.put("ownerId", userId);
			param.put("functionId", moduleId);
			param.put("ownerType", 1);
			param.put("functionType", 1);
			params.add(param);
		}
		this.addBatch("addAllUserPermission", params);
	}

	public void delAllUserActions(int userId) {
		this.deleteObject("delAllUserActions", userId);
	}

	public void addAllUserActions(int userId, List<Integer> actionIds,
			int updateUser, Date updateTime) {
		List<Map<String,Object>> params = new ArrayList<Map<String,Object>>();
		for(Integer actionId : actionIds){
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("createUser", updateUser);
			param.put("createTime", updateTime);
			param.put("ownerId", userId);
			param.put("functionId", actionId);
			param.put("ownerType", 1);
			param.put("functionType", 2);
			params.add(param);
		}
		this.addBatch("addAllUserPermission", params);
	}

}
